package com.mingmen.canting.zhenban;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;

import com.mingmen.canting.adapter.IndexAdapter;
import com.mingmen.canting.adapter.MineRadioAdapter;
import com.mingmen.canting.banner.views.BannerViewPager;
import com.mingmen.canting.caigourenyuan.CaiGouRenyuanActivity;
import com.mingmen.canting.chushizhang.ChuShiZhangCaiGouDanActivity;
import com.mingmen.canting.definerecycleview.SwipeRecyclerView;
import com.mingmen.canting.flycotablelayout.flyco.tablayout.SlidingTabLayout;
import com.mingmen.canting.flycotablelayout.flyco.tablayout.listener.OnTabSelectListener;
import com.mingmen.canting.flycotablelayout.flyco.tablayout.widget.MsgView;
import com.mingmen.canting.gongyingshang.GongYingShangActivity;
import com.mingmen.canting.gongyingshang.SupplierDaTingActivity;
import com.mingmen.canting.gongyingshang.SupplierShopActivity;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.http.callback.StringNoCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.immersionbar.ImmersionBar;

import com.mingmen.canting.jingli.JingLiNewCaiGouDanActivity;
import com.mingmen.canting.model.MyLiveList;
import com.mingmen.canting.personal.PersonalSettingsActivity;
import com.mingmen.canting.util.DividerItemDecoration;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;


public class IndexFragment extends Fragment implements View.OnClickListener {

    SwipeRecyclerView rv_index;

    private IndexAdapter mRadioAdapter = null;
    private GridLayoutManager mLinearLayoutManager;
    private List<MyLiveList> mList = new ArrayList<>();

    LinearLayout layout_caigou;
    ImageView img_banner;
    LinearLayout layout_jingli;
    LinearLayout layout_caigoudan;
    LinearLayout layout_caigourenyuan;
    BannerViewPager banner_3d;
    List<String> urlList = new ArrayList<>();
    String token;

    LinearLayout layout_personal;

    LinearLayout layout_personalsettings;
    String roleOrderApply;
    String roleOrderAudit;
    String roleOrderBuyer;
    String roleOrderLobby;
    @BindView(R.id.linear_return)
    LinearLayout linear_return;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_index, null);
        rv_index = view.findViewById(R.id.rv_index);
        layout_personal = view.findViewById(R.id.layout_personal);
        layout_personal.setOnClickListener(this);
        layout_personalsettings = view.findViewById(R.id.layout_personalsettings);
        layout_personalsettings.setOnClickListener(this);
        initData();
        roleOrderApply = (String) SharePreferenceUtil.get(getActivity(), "roleOrderApply", "");
        roleOrderAudit = (String) SharePreferenceUtil.get(getActivity(), "roleOrderAudit", "");
        roleOrderBuyer = (String) SharePreferenceUtil.get(getActivity(), "roleOrderBuyer", "");
        roleOrderLobby = (String) SharePreferenceUtil.get(getActivity(), "roleOrderLobby", "");
        Log.d("roleOrderLobby", roleOrderLobby);
        ImmersionBar.with(this).titleBar(linear_return)
                .navigationBarColor(R.color.black).init();
        token = (String) SharePreferenceUtil.get(getActivity(), "token", "");
        Log.d("token", token);

        banner_3d = view.findViewById(R.id.banner_3d);
        layout_caigou = view.findViewById(R.id.layout_caigou);
        layout_caigou.setOnClickListener(this);
        img_banner = view.findViewById(R.id.img_banner);
        layout_jingli = view.findViewById(R.id.layout_jingli);
        layout_jingli.setOnClickListener(this);
        layout_caigoudan = view.findViewById(R.id.layout_caigoudan);
        layout_caigoudan.setOnClickListener(this);
        layout_caigourenyuan = view.findViewById(R.id.layout_caigourenyuan);
        layout_caigourenyuan.setOnClickListener(this);
        Glide.with(getActivity()).load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596880349544&di=041aca87874cb6959aa1c85c9bcdd50b&imgtype=0&src=http%3A%2F%2Ffile.digitaling.com%2FeImg%2Fuimages%2F20150824%2F1440412608821067.jpg").into(img_banner);
        getunLock();
        urlList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586013578881&di=3fe60f355e63e0b15aa59e6f3597b2a8&imgtype=0&src=http%3A%2F%2Fphoto.16pic.com%2F00%2F34%2F07%2F16pic_3407117_b.jpg");
        urlList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597663936753&di=cc0c6dfea48fe2a73bd99c3364a0c063&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F14%2F75%2F01300000164186121366756803686.jpg");
        urlList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597663936753&di=17732eed54bd103280977a7e81c711b7&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F36%2F48%2F19300001357258133412489354717.jpg");
        urlList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597663936753&di=8e5fac882b1a97eb84b2a0de5f8e2710&imgtype=0&src=http%3A%2F%2Fa1.att.hudong.com%2F05%2F00%2F01300000194285122188000535877.jpg");
        banner_3d.initBanner(urlList, true)//开启3D画廊效果
                .addPageMargin(10, 50)//参数1page之间的间距,参数2中间item距离边界的间距
                .addPointMargin(6)//添加指示器
                .addStartTimer(8)//自动轮播5秒间隔
                .addPointBottom(7)
                .addRoundCorners(20)//圆角
                .finishConfig()//这句必须加
                .addBannerListener(new BannerViewPager.OnClickBannerListener() {
                    @Override
                    public void onBannerClick(int position) {
                        //点击item
//                        showShort("效果1点击"+position);
                        Log.i("test", "--------------00x1");
                    }
                });

        return view;
//        return super.onCreateView(inflater, container, savedInstanceState);
    }

    private void initData() {
        mRadioAdapter = new IndexAdapter(getActivity());
        mLinearLayoutManager = new GridLayoutManager(getActivity(), 3);
        rv_index.setLayoutManager(mLinearLayoutManager);
        DividerItemDecoration itemDecorationHeader = new DividerItemDecoration(getActivity(), DividerItemDecoration.HORIZONTAL_LIST);
//        rv_index.setDividerDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.divider_main_bg_height_1));
//        mRecyclerview.addItemDecoration(itemDecorationHeader);

        rv_index.setAdapter(mRadioAdapter);
        for (int i = 0; i < 30; i++) {
            MyLiveList myLiveList = new MyLiveList();
            myLiveList.setTitle("这是第" + i + "个条目");
            myLiveList.setSource("来源" + i);
            mList.add(myLiveList);
            mRadioAdapter.notifyAdapter(mList, false);
        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();


    }


    @Override
    public void onClick(View v) {
        roleOrderApply = (String) SharePreferenceUtil.get(getActivity(), "roleOrderApply", "");
        roleOrderAudit = (String) SharePreferenceUtil.get(getActivity(), "roleOrderAudit", "");
        roleOrderBuyer = (String) SharePreferenceUtil.get(getActivity(), "roleOrderBuyer", "");
        roleOrderLobby = (String) SharePreferenceUtil.get(getActivity(), "roleOrderLobby", "");
        switch (v.getId()) {
            case R.id.layout_caigou:
                if (roleOrderApply.equals("1")) {
                    startActivity(new Intent(getActivity(), ZhenBanCaiGouDanActivity.class));
                } else {
                    Toast.makeText(getActivity(), "无砧板权限", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.layout_jingli:

                if (roleOrderLobby.equals("1")) {
                    startActivity(new Intent(getActivity(), JingLiNewCaiGouDanActivity.class));
                } else {
                    Toast.makeText(getActivity(), "无经理权限", Toast.LENGTH_SHORT).show();
//                    startActivity(new Intent(getActivity(), JingLiNewCaiGouDanActivity.class));
                }

                break;
            case R.id.layout_caigoudan:
                if (roleOrderAudit.equals("1")) {
                    startActivity(new Intent(getActivity(), ChuShiZhangCaiGouDanActivity.class));
                } else {
                    Toast.makeText(getActivity(), "无厨师长权限", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.layout_caigourenyuan:
                if (roleOrderBuyer.equals("1")) {
                    startActivity(new Intent(getActivity(), CaiGouRenyuanActivity.class));
                } else {
                    Toast.makeText(getActivity(), "无采购人员", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.layout_personal:
                startActivity(new Intent(getActivity(), GongYingShangActivity.class));

                break;
            case R.id.layout_personalsettings:
                startActivity(new Intent(getActivity(), PersonalSettingsActivity.class));
                break;

        }
    }


    public void getunLock() {
        HttpResponse.setUnLock(token, new StringNoCallback(getActivity()) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Log.d("onDestroy", s);
                Log.d("onDestroy", "取消心跳");

            }
        });
    }
}
